package com.template.controller.websocket.service;

import com.template.controller.websocket.dto.MessageR;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class WsService {

    private final SimpMessagingTemplate messagingTemplate;

    public void notifyFrontend(String message) {
        MessageR messageR = new MessageR(message);
        log.warn("广播消息：{}", messageR);
        messagingTemplate.convertAndSend("/topic/simp-to", messageR);
    }

    public void sendToOther(String userId, String message) {
        MessageR messageR = new MessageR(message);
        log.warn("发送给指定用户：userId = {}, message = {}", userId, message);
        messagingTemplate.convertAndSendToUser(userId, "/topic/simp-to-user", messageR);
    }
}
